package com.auction.system.action.login;

import com.auction.system.constant.SystemConstant;
import com.auction.system.domain.dto.user.AdminInfo;
import com.auction.system.domain.query.auction.AuctionGoodQuery;
import com.auction.system.domain.query.auction.OrderComplaintQuery;
import com.auction.system.domain.query.auction.OrderInfoQuery;
import com.auction.system.domain.query.auction.SysNewsQuery;
import com.auction.system.domain.query.user.AdminInfoQuery;
import com.auction.system.domain.query.user.UserInfoQuery;
import com.auction.system.enums.EnumAuctionStatus;
import com.auction.system.enums.EnumHandleStatus;
import com.auction.system.enums.EnumNewsType;
import com.auction.system.service.auction.IAuctionGoodService;
import com.auction.system.service.auction.IOrderComplaintService;
import com.auction.system.service.auction.IOrderInfoService;
import com.auction.system.service.auction.ISysNewsService;
import com.auction.system.service.user.IAdminInfoService;
import com.auction.system.service.user.IUserInfoService;
import com.auction.system.utils.ServeltUtil;
import com.auction.system.utils.StringUtil;
import com.auction.system.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @ClassName AdminLoginAction
 * @Author 王伟学
 * @Version 1.0
 **/
@Controller
@Slf4j
@RequestMapping("/back/admin")
public class AdminLoginAction {

    @Autowired
    private IAdminInfoService adminInfoService;

    @Autowired
    private ISysNewsService sysNewsService;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IAuctionGoodService auctionGoodService;

    @Autowired
    private IOrderInfoService orderInfoService;

    @Autowired
    private IOrderComplaintService orderComplaintService;

    /**
     * 登录页面
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "admin_login";
    }

    /**
     * 登录
     * @param loginAccount
     * @param loginPassword
     * @return
     */
    @RequestMapping(value = "/main",method = RequestMethod.POST)
    public ModelAndView index(@RequestParam("loginAccount") String loginAccount, @RequestParam("loginPassword") String loginPassword) {
        ModelAndView mav = new ModelAndView();
        try{
            AdminInfoQuery adminInfoQuery = new AdminInfoQuery();
            adminInfoQuery.setNickName(loginAccount);
            adminInfoQuery.setUserPassword(loginPassword);
            List<AdminInfo> adminInfoList = adminInfoService.getList(adminInfoQuery);
            if(StringUtil.isEmpty(adminInfoList)){
                log.info("查无此账号:"+loginAccount);
                mav.addObject(SystemConstant.ERROR_KEY, "输入的账号或密码错误");
                mav.setViewName("admin_login");
                return mav;
            }
            AdminInfo adminInfo = adminInfoList.get(0);
            adminInfoService.edit(adminInfo);
            //将userAccount存到session中
            ServeltUtil.getSession().setAttribute(SystemConstant.USERID_KEY,adminInfo.getId());
            mav.addObject("adminInfo",adminInfo);
            mav.setViewName("admin_index");
        }catch (Exception e){
            log.info("系统异常:" + loginAccount);
            mav.addObject(SystemConstant.ERROR_KEY, "系统异常");
            mav.setViewName("admin_login");
        }
        return mav;
    }

    @RequestMapping("/welcome")
    public ModelAndView welcome(){
        ModelAndView mav = new ModelAndView();
        //获取管理员信息
        AdminInfo adminInfo = adminInfoService.get(Long.parseLong(UserUtil.getUserId()));
        //待发布新闻数
        SysNewsQuery sysNewsQuery = new SysNewsQuery();
        sysNewsQuery.setNewsType(EnumNewsType.TO_BE_ANNOUNCED.getCode());
        mav.addObject("sysNewsNum",sysNewsService.getList(sysNewsQuery).size());
        mav.addObject("adminInfo",adminInfo);
        //管理员数
        mav.addObject("adminNum",adminInfoService.getList(new AdminInfoQuery()).size());
        //用户数
        mav.addObject("userNum",userInfoService.getList(new UserInfoQuery()).size());
        //正在拍卖拍品数
        AuctionGoodQuery auctionGoodQuery = new AuctionGoodQuery();
        auctionGoodQuery.setAuctionStatus(EnumAuctionStatus.BIDDING.getCode());
        mav.addObject("bidGoodNum",auctionGoodService.getList(auctionGoodQuery).size());
        //订单数
        mav.addObject("orderNum",orderInfoService.getList(new OrderInfoQuery()).size());
        //待处理投诉数
        OrderComplaintQuery orderComplaintQuery = new OrderComplaintQuery();
        orderComplaintQuery.setHandleStatus(EnumHandleStatus.TO_HANDLE.getCode());
        mav.addObject("toHandleNum",orderComplaintService.getList(orderComplaintQuery).size());
        mav.setViewName("system/admin_welcome");
        return mav;
    }

    /**
     * 退出
     * @return
     */
    @RequestMapping(value = "/logout",method = RequestMethod.GET)
    public ModelAndView logout() {
        ModelAndView mav = new ModelAndView();
        try{
            String userId = UserUtil.getUserId();
            if(StringUtil.isEmpty(userId)){
                log.info("退出系统失败，原因：获取的userId为空");
                mav.addObject(SystemConstant.ERROR_KEY, "退出系统失败，原因：获取的userId为空");
                mav.setViewName("admin_login");
                return mav;
            }
        }catch (Exception e){
            log.error("退出系统异常",e);
            mav.addObject(SystemConstant.ERROR_KEY, "退出系统失败");
        }
        mav.setViewName("admin_login");
        return mav;
    }

}
